import { Injectable } from '@angular/core';
import {Http, Response, Headers, RequestOptions, URLSearchParams} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {ExamSubmitInfo} from "./exam-submit.class";


@Injectable()
export class EAService {
  private employeeListUrl = '/water/enterpriseinfo/getPersonOfEnterprise';
    //TODO 修改为真正的URL地址

    constructor(private _http: Http) { }

  getUserinfo(url): Promise<any> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.get(url).toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  getUserInfoOfCompany(url: string, ids: string): Promise<any> {
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});

    return this._http.post(url, JSON.stringify({'ids': ids}), options).toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  reviewAppr(url: string, pkid: string): Promise<any> {
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});

    return this._http.post(url, JSON.stringify({'pkid': pkid}), options).toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  submitUpdate(url: string, info: any): Promise<any> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(url, JSON.stringify(info), options).toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  submit(url: string, info: ExamSubmitInfo): Promise<any> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(url, JSON.stringify(info), options).toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  deleteAppr(url: string, pkids: any[]): Promise<any> {
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});

    return this._http.post(url, JSON.stringify({'pkids': pkids}), options).toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  extractData(res: Response) {
    let body = res.json();
    return body || { };
  }

  handleError(error: Response | any) {
    let errMsg: string;
    if (error instanceof Response) {
      if (error.status > 200) {
        errMsg = '网络异常';
      } else {
        errMsg = '网络异常';
      }
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    return Promise.reject(errMsg);
  }


  getEmpolyeesInfo(): Promise<any> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this.employeeListUrl, {}, options).toPromise()
    .then(res => {
       return  res.json().data;
    });
  }
}
